package fgame.graphic
{
	import fgame.resource.Model3DCache;
	import fgame.unit.Actor3D;
	
	import flare.core.Label3D;
	import flare.core.Pivot3D;
	import flare.utils.Pivot3DUtils;
	
	import flash.geom.Vector3D;
	
	import ricky.Interface.IAnimate;
	import ricky.Interface.IGraphic;
	import ricky.game.Actor;
	import ricky.loader.Request;
	
	public class Graphic extends Pivot3D implements IGraphic
	{
		public function Graphic(actor3d:Actor3D)
		{
			super("");
			_actor3d=actor3d;
			_labelNames=new Vector.<String>();
			_label=new Object();
		}
		
		
		///////////////////////////////////////////////////////////////////////////
		//****************************公共方法*************************************
		///////////////////////////////////////////////////////////////////////////
		
		public function get currModel():String
		{
			return _currModel;
		}

		public function get label():Object
		{
			return _label;
		}

		public function set label(value:Object):void
		{
			_label = value;
		}

		override public function get frameSpeed():Number
		{
			if(_main)
				_frameSpeed=_main.frameSpeed;
			return _frameSpeed;
		}

		override public function set frameSpeed(value:Number):void
		{
			super.frameSpeed=_frameSpeed;
			if(_main)
				_main.frameSpeed=_frameSpeed;
		}

		public function get main():Pivot3D
		{
			return _main;
		}
		
		public function setChildScale(scaleX:Number=1,scaleY:Number=1,scaleZ:Number=1):void
		{
			if(main)
			{
				this.main.scaleX=scaleX;
				this.main.scaleX=scaleY;
				this.main.scaleX=scaleZ;
			}
			
			_scaleX=_scaleX;
			_scaleY=scaleY;
			_scaleZ=scaleZ;
		}

//		/**
//		 * 逻辑主体
//		 */
//		public function get actor3d():Actor3D
//		{
//			return Actor3D(_actor3d);
//		}
		
		///////////////////////////////////////////////////////////////////////////
		//****************************接口实现*************************************
		///////////////////////////////////////////////////////////////////////////
		
		/**
		 * 得到某个属性的值
		 */
		public function getAttribute(pro:String):*
		{
			return this[pro];
		}
		
		/**
		 * 设置某个属性的值
		 */
		public function setAttribute(pro:String,value:*):void
		{
			this[pro]=value;
		}
		
		/**
		 * 加载模型资源
		 */
		public function wear(model:String=null,name:String=null,onLoaded:Function=null):void
		{
			_onLoaded=onLoaded;
			if(name==null)
				name="main";
			if(name=="main")
			{
				_currModel=model;
			}
			clear(name);

			Model3DCache.instance.getResource(_currModel,onModelComplete,2,name);
		}
		
		/**
		 * 追加骨骼动画
		 * @param model:String 加载的url
		 * @param name:String  要添加到的模型
		 * @param onLoaded:Function onLoaded(graphic)
		 */
		public function appendAnimation(model:String=null,name:String=null,onLoaded:Function=null):void
		{
			_onAniLoaded=onLoaded;
			if(name==null)
			{
				name="main";
			}

			Model3DCache.instance.getResource(model,onAnimationComplete,1,name);
	
		}
		
		/**
		 * 清理指定名字的模型和动画
		 */	
		public function clear(name:String=null):void
		{
			var p:Pivot3D=this.getChildByName(name);
			if(p)
				this.removeChild(p);
		}
		
		/**
		 * 对应的游戏逻辑对象
		 */
		public function get actor():Actor
		{
			return _actor3d;
		}
		
		/**
		 * @private
		 */
		public function set actor(value:Actor):void
		{
			_actor3d = value;
		}
		
		/**
		 * 播放动画
		 */
		public function playScene(par:*=null):void
		{
			if(_main)
			{
				if(par)
					_main.gotoAndPlay(par);
				else
					_main.play();
			}
		}
		
		/**
		 * 停止播放
		 */
		override public function stop():void
		{
			if(_main)
				_main.stop();
		}
		
		public function get velocityPu():Vector3D
		{
			return _velocityPu;
		}
		
		public function set velocityPu(value:Vector3D):void
		{
			_velocityPu = value;
		}
		
		public function get rotationPu():Vector3D
		{
			return _rotationPu;
		}
		
		public function set rotationPu(value:Vector3D):void
		{
			_rotationPu = value;
		}
		///////////////////////////////////////////////////////////////////////////
		//***************************protected*************************************
		///////////////////////////////////////////////////////////////////////////
		
		protected function onModelComplete(e:Request):void
		{
			var mesh:Pivot3D=e.data.clone();
			this.addChild(mesh);

			if(e.head)
				mesh.name=e.head.toString();
			if(e.head=="main")
			{
				_main=mesh;
				_labelNames.length=0;
				for each(var la:Object in main.labels)
				{
					_labelNames.push(la.name);
					_label[la.name]=la;
//					Debug.traceMe(la.name,"动画");
				}
			}
			
//			Debug.traceMe(mesh.name,"模型加载完成！");
			
			this.scaleX=_scaleX;
			this.scaleX=_scaleY;
			this.scaleX=_scaleZ;
						
			if(this.actor.data)
			{
				var data:Object=actor.data;
				var skeleton:String=data.skeleton;
				if(skeleton)
					appendAnimation(skeleton);
				else if(_labelNames.length>0)
				{
					_main.gotoAndPlay(_labelNames[0]);
				}else
				{
					_main.play();
				}
			}
			
			
			if(_onLoaded!=null)
				_onLoaded(this);
			
			_onLoaded=null;
		}
		
		/**
		 * 骨骼加载完成
		 */
		protected function onAnimationComplete(r:Request):void
		{
			var skeleton:Pivot3D=r.data;
			skeleton.name=r.key;
			
			var appName:String=r.head;
			
			var char:Pivot3D= this.getChildByName(appName); //Model3DCache.instance.getValue(_currModel);

//			Debug.traceMe(skeleton.name,"骨骼动画加载完成！");

			var ls:Vector.<Label3D>=new Vector.<Label3D>();
			if(char)
			{

				Pivot3DUtils.appendAnimation( char,skeleton);

				var child:Pivot3D=skeleton.getChildByName("labels",0,true);

				if(child)
				{
					for each(var c:Label3D in child.labels)
						ls.push(c);
				}

				if(ls.length==0)
					for each( child in skeleton.children)
						for each(var l:Label3D in child.labels)
							ls.push(l);

				for each(var l3:Label3D in ls)
				{
					if(_labelNames.indexOf(l3.name)==-1)
					{
						char.addLabel(l3,true);
						_labelNames.push(l3.name);
						_label[l3.name]=l3;
//						Debug.traceObj(this,l3,"添加label");
					}
				}

				if(char==this.main)
					main.frameSpeed=this._frameSpeed;
				
				var aniName:String="idle";
				for each(var la:Object in main.labels)
				{
//					Debug.traceMe(la,"labels.name="+la.name);
					aniName=la.name;
					if(la.name=="idle")
					{
						break;
					}
				}
				
				char.gotoAndPlay(aniName);
			}
			
			if(_onAniLoaded!=null)
				_onAniLoaded(this);
				
			_onAniLoaded=null;
		}

		//
		private var _frameSpeed:Number=1;
		private var _labelNames:Vector.<String>;
		private var _label:Object;
		private var _currModel:String;
		
		private var _scaleX:Number=1,_scaleY:Number=1,_scaleZ:Number=1;
		
		private var _main:Pivot3D;
		private var _actor3d:Actor;
		
		private var _onAniLoaded:Function;
		private var _onLoaded:Function;
		
		private var _rotationPu:Vector3D;
		private var _velocityPu:Vector3D;
	}
}